package com.kgc.community.controller;

import com.kgc.community.pojo.Rooms;
import com.kgc.community.service.BuildingaddService;
import com.kgc.community.utils.ExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author Mr.Man
 * @Date 2020-12-06 16:42
 **/
@Controller
public class BuildingAdd {
    @Resource
    BuildingaddService buildingaddService;


    @RequestMapping("addbulding.do")
    public String addbulding(){
        return "addbulding";
    }
    @RequestMapping("addbuildingmore.do")
    public String addbuildingmore(@RequestParam("file") CommonsMultipartFile file, Model model) throws IOException {
        String path="E:/"+new Date().getTime()+file.getOriginalFilename();
        File newFile=new File(path);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(newFile);

        ExcelUtil excelUtil = new ExcelUtil();
        List<List<String>> list = excelUtil.readWork(newFile);
        for (int i = 1; i < list.size(); i++) {
            List<String> strings = list.get(i);
            Rooms rooms = new Rooms();
            rooms.setId(strings.get(0));
            rooms.setCount(3);
            rooms.setArea(Double.parseDouble(strings.get(3)));
            rooms.setDescribes(strings.get(2));
            rooms.setStatus(Integer.parseInt(strings.get(4)));
            rooms.setUserId(Integer.parseInt(strings.get(5)));
            rooms.setBuildingId(Integer.valueOf(strings.get(1)));
            int i1 = buildingaddService.addRooms(rooms);
        }
        model.addAttribute("msg","上传成功");
        return "addbulding";
    }
}
